﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.SingleArgumentOperator"/>.
    /// </summary>
    public abstract class SingleArgumentOperatorVM : OperatorVM
    {
        #region /* FIELDS */

        protected ExpressionVM _expressionVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.SingleArgumentOperator"/>.
        /// </summary>
        protected SingleArgumentOperatorVM(SingleArgumentOperator singleArgumentOperator, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(singleArgumentOperator, dictionary)
        {
            _expressionVM = (ExpressionVM)CreateVM(singleArgumentOperator.Expression, false, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.SingleArgumentOperator"/> model.
        /// </summary>
        public SingleArgumentOperator SingleArgumentOperator
        {
            get { return (SingleArgumentOperator)CodeObject; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public override void RenderExpression(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            if (!flags.HasFlag(RenderFlags.NoBorder))
                passFlags |= RenderFlags.ForceBorder;
            bool hasParens = (SingleArgumentOperator.HasArgumentParens && !MethodDeclBaseVM.HideMethodParens);

            RenderOperator(renderer, flags);
            renderer.RenderText(hasParens ? Expression.ParseTokenStartGroup : " ", PUNC_BRUSH, this);
            if (_expressionVM != null)
                _expressionVM.Render(renderer, passFlags);
            if (hasParens)
                renderer.RenderText(Expression.ParseTokenEndGroup, PUNC_BRUSH, this);
        }

        public override void RenderVisible(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderVisible(renderer, flags);
            if (_expressionVM != null)
                _expressionVM.RenderVisible(renderer, flags);
        }

        public override void UnRender()
        {
            if (_expressionVM != null)
                _expressionVM.UnRender();
            base.UnRender();
        }

        #endregion
    }
}
